#pragma once

#include <string>

#include "ft2build.h"
#include FT_FREETYPE_H

#include "TrueText/Error.h"

#ifdef ENABLE_TTF_LOG
    #include <Windows.h>

	#define TTF_Log(fmt, ...) { \
		char buffer[2000];
		sprintf(buffer, fmt, __VA_ARGS__);
		OutputDebugString(buffer);
	}
#else
	#define TTF_Log(fmt, ...) ((void)0)
#endif

void truetext_lib_assert(const char * _Message, const char *_File, unsigned _Line);

#ifdef  NDEBUG

#define TT_ASSERT(_Expression)    ((void)0)
#define TT_FAIL(_Expression)      ((void)0)
#define TT_FAIL_UNEXPECTED()		((void)0)

#else

#define TT_ASSERT(_Expression) (void)( (!!(_Expression)) || (truetext_lib_assert(#_Expression, __FILE__, __LINE__), 0) )
#define TT_FAIL(_Expression) (void)(truetext_lib_assert(_Expression, __FILE__, __LINE__) )
#define TT_FAIL_UNEXPECTED()		(void)(truetext_lib_assert("Unexpected failure", __FILE__, __LINE__) )

#endif  /* NDEBUG */

std::string LookupFreeTypeErrorMessageA(FT_Error error);
std::wstring LookupFreeTypeErrorMessageW(FT_Error error);

#define TRUETEXT__TEXT(quote) L##quote
#define TRUETEXT_TEXT(quote) TRUETEXT__TEXT(quote)

#if defined(_WIN32) && !defined(SAMSUNG_VERSION) && !defined(SAMSUNG_BADA)

#include <strsafe.h>

#define FT_EH(expr) { \
	FT_Error fterror_for_ft_check = (expr); \
	if (fterror_for_ft_check != FT_Err_Ok) \
		{ \
            wchar_t errorMessage[1000]; \
            StringCbPrintfW(errorMessage, 1000, L"FreeType error: 0x%02X - %s at %s:%d", \
                fterror_for_ft_check, \
                LookupFreeTypeErrorMessageW(fterror_for_ft_check).c_str(), \
                TRUETEXT_TEXT(__FILE__), \
                __LINE__); \
            \
			TTF_Log("%S", errorMessage); \
            throw TrueText::Error(errorMessage); \
		} \
	}
#elif defined(ANDROID_NDK)

#include "swprintf.h"

void TrueText_Android_Log(char* msg);

#define FT_EH(expr) { \
	FT_Error fterror_for_ft_check = (expr); \
	if (fterror_for_ft_check != FT_Err_Ok) \
		{ \
            char errorMessage[1000]; \
            sprintf(errorMessage, "FreeType error: 0x%02X - %s at %s:%d", \
                fterror_for_ft_check, \
                LookupFreeTypeErrorMessageA(fterror_for_ft_check).c_str(), \
                __FILE__, \
                __LINE__); \
            \
			TrueText_Android_Log(errorMessage); \
		} \
	}
#elif defined(SAMSUNG_BADA) || defined(SAMSUNG_VERSION)

#include <FBase.h>

#define FT_EH(expr) { \
	FT_Error fterror_for_ft_check = (expr); \
	if (fterror_for_ft_check != FT_Err_Ok) \
		{ \
			AppLogException("FreeType error: 0x%02X - %s at %s:%d", \
                fterror_for_ft_check, \
                LookupFreeTypeErrorMessageA(fterror_for_ft_check).c_str(), \
                __FILE__, \
                __LINE__); \
		} \
	}
#elif defined(IPHONE_VERSION) || defined(MARMALADE)

#define FT_EH(expr) { \
FT_Error fterror_for_ft_check = (expr); \
if (fterror_for_ft_check != FT_Err_Ok) \
{ \
printf("FreeType error: 0x%02X - %s at %s:%d", \
fterror_for_ft_check, \
LookupFreeTypeErrorMessageA(fterror_for_ft_check).c_str(), \
__FILE__, \
__LINE__); \
} \
}
#endif

void InitLibraryHandle();
void CloseLibraryHandle();
FT_Library GetLibraryHandle();

const int DPI_Horizontal = 72;
const int DPI_Vertical = 72;
const int FT_Units_In_Pixel = 64;
